package com.xzq.httptest;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class LoginActivity extends AppCompatActivity {

    private TextView responseBody;
    private EditText verifyCode;

    private Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        setContentView(R.layout.activity_login);

        Button getCode = findViewById(R.id.get_code);
        Button login = findViewById(R.id.登录);
        responseBody = findViewById(R.id.response_body);
        verifyCode = findViewById(R.id.verify_code_input);

        getCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    getCodeResponse();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    login();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        handler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 1) {
                    UserModel model = (UserModel) msg.obj;
                    String token = model.getData().getAccess_token();
                    SharedPreferences sharedPreferences = getSharedPreferences("userToken", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString("token", token);
                    editor.apply();
                }
            }
        };

    }

    private void getCodeResponse() throws IOException {
        String url = "http://api.myas.maissy.net/api/verify-code";
        Response response;
        RequestBody body = new FormBody.Builder()
                .add("mobile", "18142808155")
                .add("access_token", "18142808155")
                .add("mobile_rule", "mobile_required")
                .build();
        response = HttpUtil.post(url, body);
        ResponseBody body1 = response.body();
        String content = response.body().string();
        LogUtils.i(content);
        CodeModel model = new Gson().fromJson(content, CodeModel.class);
        responseBody.setText(model.getMessage());
    }

    private void login() throws IOException {
        String url = "http://api.myas.maissy.net/api/api_user/sms_login";
        Response response;
        RequestBody body = new FormBody.Builder()
                .add("mobile", "18142808155")
                .add("verify_code", verifyCode.getText().toString())
                .build();
        response = HttpUtil.post(url, body);
        String content = response.body().string();
        UserModel model = new Gson().fromJson(content, UserModel.class);
        Message message = new Message();
        message.what = 1;
        message.obj = model;
        handler.handleMessage(message);
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        finish();
    }
}